package com.yc.star.booksystem.service;

import com.yc.star.booksystem.enums.BookStatusEnum;
import com.yc.star.booksystem.mapper.BookMapper;
import com.yc.star.booksystem.model.BookInfo;
import com.yc.star.booksystem.model.PageRequest;
import com.yc.star.booksystem.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    public BookInfo queryBookById(Integer id) {
        return bookMapper.queryBookById(id);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        // 拿到数据库总数
        Integer count = bookMapper.getCount();
        // 拿到查询的图书列表
        List<BookInfo> bookList = bookMapper.getBookList(pageRequest);
        for (BookInfo book : bookList) {
            book.setStateCN(BookStatusEnum.getNameByCode(book.getStatus()).getDesc());
        }
        PageResult<BookInfo> pageResult = new PageResult<>();
        pageResult.setCount(count);
        pageResult.setRecords(bookList);
        pageResult.setPageRequest(pageRequest);
        return pageResult;
    }

    public void addBook(BookInfo bookInfo) {
        bookMapper.insertBook(bookInfo);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public void batchDelete(List<Integer> ids) {
        bookMapper.batchDelete(ids);
    }
}
